package Servlet;

import Bean.Ticket;
import Bean.User;
import Service.TicketService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 张
 * Date: 2025-03-12
 * Time: 21:30
 */
//机票退票申请
@WebServlet("/QuitTicketServlet")
public class QuitTicketServlet extends HttpServlet {
    private TicketService ticketService = new TicketService();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("application/json;charset=utf8");
        String username = req.getParameter("jptg-name");
        //退款机票对应的航班号
        String flightNumber = req.getParameter("flightNumber");
        String seatNumber = req.getParameter("seatNumber");
        System.out.println(seatNumber);
        //验证身份
        HttpSession session = req.getSession();
        User loginUser = (User) session.getAttribute("LoginUser");
        if(!loginUser.getName().equals(username)) {
            req.setAttribute("status", "error");
            req.setAttribute("message", "用户信息错误!!!");
            req.setAttribute("redirectUrl", "user_index.jsp");
            req.setAttribute("waitSeconds", 3);
            req.getRequestDispatcher("skip.jsp").forward(req,resp);
        }
        int ret = ticketService.quit(loginUser.getUserId(),flightNumber,seatNumber);
        if(ret == 2) {
            req.setAttribute("status", "error");
            req.setAttribute("message", "订单不存在!!!");
            req.setAttribute("redirectUrl", "user_index.jsp");
            req.setAttribute("waitSeconds", 3);
            req.getRequestDispatcher("skip.jsp").forward(req,resp);
        }
        //改签成功就提示一下
        req.setAttribute("status", "success");
        req.setAttribute("message", "提交申请成功!!");
        req.setAttribute("redirectUrl", "user_index.jsp");
        req.setAttribute("waitSeconds", 3);
        req.getRequestDispatcher("skip.jsp").forward(req,resp);
    }
}
